package com.htc.comp;

import java.util.List;

import com.htc.book.Book;
import com.htc.book.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class BookAdapter extends BaseAdapter{

	private Context context;
	private List<Book> books;
	
	public BookAdapter(Context ctx, List<Book> books){
		this.context = ctx;
		this.books = books;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return books.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return books.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		BookHolder holder = new BookHolder();
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = (View)inflater.inflate(R.layout.book_item, parent);
			
			holder.bookImage  = (ImageView)convertView.findViewById(R.id.ivBookImage);
			holder.bookName = (TextView)convertView.findViewById(R.id.tvBookName);
			holder.bookPublisher = (TextView)convertView.findViewById(R.id.tvBookPublisher);
			holder.favorite = (ImageButton)convertView.findViewById(R.id.ibFavorite);
		}
		
		// download image and set for book image
		holder.bookName.setText(books.get(position).getTitle());
		holder.bookPublisher.setText(books.get(position).getPublisher());	
		
		
		return convertView;
	}

	private class BookHolder{
		ImageView bookImage;
		TextView bookName;
		TextView bookPublisher;
		ImageButton favorite;
	}
}
